package org.code.clienteweb;

import java.rmi.RemoteException;
import java.util.ArrayList;
import java.util.List;
import java.util.StringTokenizer;

import org.tempuri.Service1SoapProxy;

public class GestionGeneral {

	Service1SoapProxy s;
	
	public GestionGeneral(){
		s=new Service1SoapProxy();
	}
	
	public List<Restaurante> listarRestaurantes(){
		try {
			String[] lista=s.listarRestaurantes();
			if(lista!=null){
				List<Restaurante> restaurantes=new ArrayList<Restaurante>();
				for(int i=0;i<lista.length;i++){
					StringTokenizer rest=new StringTokenizer(lista[i],"|");
					restaurantes.add(new Restaurante(Integer.parseInt(rest.nextToken()),rest.nextToken(),rest.nextToken(),rest.nextToken(),Double.parseDouble(rest.nextToken()),Double.parseDouble(rest.nextToken())));
				}
				return restaurantes;
			}
		} catch (RemoteException e) {
			e.printStackTrace();
		}
		return null;
	}
	
	public List<Comentario> listarComentariosRestaurante(int idRestaurante){
		try {
			String[] lista=s.listarComentariosRestaurante(idRestaurante);
			if(lista!=null){
				List<Comentario> restaurantes=new ArrayList<Comentario>();
				for(int i=0;i<lista.length;i++){
					StringTokenizer rest=new StringTokenizer(lista[i],"|");
					restaurantes.add(new Comentario(rest.nextToken(),rest.nextToken(),Integer.parseInt(rest.nextToken()),Integer.parseInt(rest.nextToken())));
				}
				return restaurantes;
			}
		} catch (RemoteException e) {
			e.printStackTrace();
		}
		return null;
	}

	public List<Menu> consultarMenu(int idRestaurante){
		try {
			String[] lista=s.consultarMenu(idRestaurante);
			if(lista!=null){
				List<Menu> menus=new ArrayList<Menu>();
				for(int i=0;i<lista.length;i++){
					StringTokenizer rest=new StringTokenizer(lista[i],"|");
					menus.add(new Menu(rest.nextToken(),Double.parseDouble(rest.nextToken().replace(',', '.')),idRestaurante));
				}
				return menus;
			}
		} catch (RemoteException e) {
			e.printStackTrace();
		}
		return null;
	}
	
	public List<Carta> consultarCarta(int idRestaurante){
		try {
			String[] lista=s.consultarCarta(idRestaurante);
			if(lista!=null){
				List<Carta> cartas=new ArrayList<Carta>();
				for(int i=0;i<lista.length;i++){
					StringTokenizer rest=new StringTokenizer(lista[i],"|");
					cartas.add(new Carta(rest.nextToken(),Double.parseDouble(rest.nextToken().replace(',','.')),rest.nextToken(),idRestaurante));
				}
				return cartas;
			}
		} catch (RemoteException e) {
			e.printStackTrace();
		}
		return null;
	}
	
	public Restaurante consultarRestaurante(int id){
		try {
			String consulta=s.consultarRestaurante(id);
			StringTokenizer aux=new StringTokenizer(consulta,"|");
			return new Restaurante(Integer.parseInt(aux.nextToken()), aux.nextToken(),aux.nextToken(),aux.nextToken(),Double.parseDouble(aux.nextToken()),Double.parseDouble(aux.nextToken()));
		} catch (RemoteException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}
}